package com.lzk.common;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.lzk.controller.front.RoomController;
import com.lzk.model.Room;
import com.lzk.model._MappingKit;

public class CommonConfig extends JFinalConfig{

	public static Prop p;
	@Override
	public void configConstant(Constants me) {
		loadConfig();
	    me.setDevMode(true);
	    me.setEncoding("UTF8");
	    me.setMaxPostSize(1024 * 1024 * 1024);
	    me.setBaseUploadPath(p.get("baseUploadPath"));
		//---------------------------------------------------
		//房间1
		Room room1 = new Room();
		room1.setId(System.currentTimeMillis() + "");
		room1.setMaxCount(5);
		room1.setRoomName("房间1");
		room1.setRoomStatus(1);
		RoomController.rooms.add(room1);
		//房间2
		Room room2 = new Room();
		room2.setId(System.currentTimeMillis() + "");
		room2.setMaxCount(5);
		room2.setRoomName("房间2");
		room2.setRoomStatus(1);
		RoomController.rooms.add(room2);
		//房间1
		Room room3 = new Room();
		room3.setId(System.currentTimeMillis() + "");
		room3.setMaxCount(5);
		room3.setRoomName("房间3");
		room3.setRoomStatus(1);
		RoomController.rooms.add(room3);
		//房间1
		Room room4 = new Room();
		room4.setId(System.currentTimeMillis() + "");
		room4.setMaxCount(5);
		room4.setRoomName("房间4");
		room4.setRoomStatus(1);
		RoomController.rooms.add(room4);
		//房间1
		Room room5 = new Room();
		room5.setId(System.currentTimeMillis() + "");
		room5.setMaxCount(5);
		room5.setRoomName("房间5");
		room5.setRoomStatus(2);
		RoomController.rooms.add(room5);
		//房间1
		Room room6 = new Room();
		room6.setId(System.currentTimeMillis() + "");
		room6.setMaxCount(5);
		room6.setRoomName("房间6");
		room6.setRoomStatus(1);
		RoomController.rooms.add(room6);
	}

	@Override
	public void configRoute(Routes me) {
	    me.scan("com.lzk.controller");
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configPlugin(Plugins me) {
		// 配置 druid 数据库连接池插件
		DruidPlugin druidPlugin = new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password"));
		me.add(druidPlugin);
				
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new WebSocketHandler("^/hallWs"));
		me.add(new WebSocketHandler("^/gameWs"));
	}
	
	
	public static DruidPlugin createDruidPlugin() {
		loadConfig();
		return new DruidPlugin(p.get("jdbcUrl"), p.get("user"), p.get("password"));
	}

	private static void loadConfig() {
		if (p == null) {
			p = PropKit.useFirstFound("config.txt");
		}
	}

}
